/*
 * 文 件 名 : HomeActivity.java
 * 版    权 : China Machi.Co.,Ltd.Copyright 2009-2011.All rights reserved
 * 描    述 : <描述>
 * 修 改 人 : lixg
 * 修改时间 : 2012-8-4
 * 跟踪单号 : <跟踪单号>
 * 修改单号 : <修改单号>
 * 修改内容 : <修改内容>
 */
package com.fire8.dolphin.ui.more;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.fire8.dolphin.model.UserInfo;
import com.fire8.dolphin.service.JaberClientService;
import com.fire8.dolphin.ui.DolphinApplication;
import com.fire8.dolphin.ui.R;
import com.fire8.dolphin.ui.imgcache.AsynLoad;
import com.fire8.dolphin.utils.AutoUpdate;
import com.fire8.dolphin.utils.LogUtil;
import com.fire8.dolphin.utils.StringUtil;
import com.fire8.dolphin.web.RequestUtils;

/**
 * 主页面
 * 显示全部问题
 *
 * @author  lixg
 * @version [版本号,2012-8-4]
 * @see     [相关类/方法]
 * @since   [产品/模块]
 */
public class MoreActivity extends Activity
{
    //private ProgressDialog progressDialog = null;
    private DolphinApplication app;
    private AsynLoad anseylodar;
    private String userId;
    private ImageView imgHead;
    private TextView tvUserName;
    private TextView tvPoint;
    private TextView tvUserTitle;
    private TextView tvRemark;
    private RelativeLayout layout;
    private View line;
    private View line1;
    private View line2;
    private View line3;
    private Button btnReg;
    private Button btnModify;
    
    private Button btnLogin;
    private Button btnNewVer;//新版本检测
    private Button btnSuggestion;//意见反馈
    private Button btnHelp;//用户指南
    private Button btnAbout;//关于
    private Button btnApplyServer;//申请咨询师
    
    private boolean success=false;
    
    private JaberClientService service;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.more);
        app = (DolphinApplication)getApplication();
        service = JaberClientService.getInstance();
        anseylodar=new AsynLoad();//异步加载头像
        String account = app.getAccount();
        imgHead = (ImageView)findViewById(R.id.img_head);
        tvUserName = (TextView)findViewById(R.id.tv_user_name);
        tvPoint = (TextView)findViewById(R.id.tv_point);
        tvUserTitle = (TextView)findViewById(R.id.tv_user_title);
        tvRemark = (TextView)findViewById(R.id.tv_user_remark);
        
        btnSuggestion = (Button)findViewById(R.id.btn_suggestion);
        btnNewVer = (Button)findViewById(R.id.btn_newversion);
        btnAbout = (Button)findViewById(R.id.btn_about);
        btnHelp = (Button)findViewById(R.id.btn_help);
        btnApplyServer = (Button)findViewById(R.id.btn_apply_server);
        
        btnSuggestion.setOnClickListener(new OnButtonListener());
        btnNewVer.setOnClickListener(new OnButtonListener());
        btnAbout.setOnClickListener(new OnButtonListener());
        btnHelp.setOnClickListener(new OnButtonListener());
        btnApplyServer.setOnClickListener(new OnButtonListener());
        
        btnModify = (Button)findViewById(R.id.btn_modify);
        btnLogin = (Button)findViewById(R.id.btn_login);
        btnReg = (Button)findViewById(R.id.btn_register);
        layout = (RelativeLayout)findViewById(R.id.lay_userinfo);
        line = (View)findViewById(R.id.v_line);
        line1 = (View)findViewById(R.id.v_line1);
        line2 = (View)findViewById(R.id.v_line2);
        line3 = (View)findViewById(R.id.v_line3);
        //如果未注册，则显示注册按钮
        if(!service.isConnected())
        {
            layout.setVisibility(View.GONE);
            line.setVisibility(View.GONE);
            btnReg.setVisibility(View.VISIBLE);
            btnReg.setOnClickListener(new OnClickListener()
            {
                
                @Override
                public void onClick(View v)
                {
                    Intent intent  = new Intent(MoreActivity.this,RegisterActivity.class);
                    startActivity(intent);
                }
            });
        }else
        {
            //否则将隐藏注册按钮，显示用户信息
            layout.setVisibility(View.VISIBLE);
            btnReg.setVisibility(View.GONE);
            line1.setVisibility(View.GONE);
            btnLogin.setVisibility(View.GONE);
            line2.setVisibility(View.GONE);
            btnModify.setVisibility(View.VISIBLE);
            //如果只是普通咨询者，则显示申请为咨询师按钮
            if(UserInfo.USER_TYPE_CLIENT.equals(Integer.valueOf(app.getUserType())))
            {
                btnApplyServer.setVisibility(View.VISIBLE);
                line3.setVisibility(View.VISIBLE);
            }
            
            userId = app.getUserId();
            new GetUserTask().execute(userId);
            
        }
        btnLogin.setOnClickListener(new OnClickListener()
        {
            
            @Override
            public void onClick(View v)
            {
                Intent intent  = new Intent(MoreActivity.this,LoginActivity.class);
                startActivity(intent);
            }
        });
        btnModify.setOnClickListener(new OnClickListener()
        {
            
            @Override
            public void onClick(View v)
            {
                
                Intent intent  = new Intent(MoreActivity.this,UserModifyActivity.class);
                startActivity(intent);
            }
        });
    }
    
    class OnButtonListener implements OnClickListener
    {
        Intent intent;
        @Override
        public void onClick(View v)
        {
            switch (v.getId())
            {
                case R.id.btn_suggestion:
                    intent  = new Intent(MoreActivity.this,SuggestionActivity.class);
                    startActivity(intent);
                    break;
                case R.id.btn_newversion:
                    AutoUpdate autoUpdate = new AutoUpdate(MoreActivity.this);
                    autoUpdate.check();
                    break;
                case R.id.btn_help:
                    intent  = new Intent(MoreActivity.this,HelpActivity.class);
                    startActivity(intent);
                    break;
                case R.id.btn_about:
                    intent  = new Intent(MoreActivity.this,AboutActivity.class);
                    startActivity(intent);
                    break;
                case R.id.btn_apply_server:
                    intent  = new Intent(MoreActivity.this,ApplyServerActivity.class);
                    startActivity(intent);
                    break;
                default:
                    break;
            }
        }
    }
    @Override
    protected void onStart()
    {
        super.onStart();
        //页面打开时，请求用户数据并显示出来
        String account = app.getAccount();
        //获取并显示用户信息
        if(service.isConnected())
        {
            layout.setVisibility(View.VISIBLE);
            btnReg.setVisibility(View.GONE);
            line1.setVisibility(View.GONE);
            btnLogin.setVisibility(View.GONE);
            line2.setVisibility(View.GONE);
            btnModify.setVisibility(View.VISIBLE);
            //如果只是普通咨询者，则显示申请为咨询师按钮
            if(UserInfo.USER_TYPE_CLIENT.equals(Integer.valueOf(app.getUserType())))
            {
                btnApplyServer.setVisibility(View.VISIBLE);
                line3.setVisibility(View.VISIBLE);
            }
            
            tvUserName.setText(app.getUserName());
            tvPoint.setText(app.getUserPoint());
            tvUserTitle.setText(app.getUserTitle());
            String remark = app.getUserRemark();
            if(StringUtil.isEmpty(remark))
            {
                tvRemark.setText("你还没有做自我介绍，请介绍一下自己吧");
            }else
            {
                tvRemark.setText(remark);
            }
            
            anseylodar.showimgAnsy(imgHead,RequestUtils.IMG_URL+app.getUserId()+".jpg");//异步加载图片
            
        }
        
        
    }
    class GetUserTask extends AsyncTask<String, Void, String>
    {
        @Override
        protected String doInBackground(String... params)
        {
            if(!RequestUtils.getInstall().isConnect(MoreActivity.this))
            {
                return "网络不可用";
            }
            String json = null;
            try
            {
                List<NameValuePair> param = new ArrayList<NameValuePair>();
                param.add(new BasicNameValuePair("userId",params[0]));
                json = RequestUtils.getInstall().getHttpJson(DolphinApplication.URL+"user/getUserInfo.php", param);
                LogUtil.d(json);
                success=true;
            }
            catch (Exception e)
            {
                LogUtil.e("" ,e);
                return "请求失败，请稍候再试";
            }
            return json;
            
        }
        @Override
        protected void onPostExecute(String result)
        {
            super.onPostExecute(result);
            if(!success)
            {
                Toast.makeText(MoreActivity.this, result, Toast.LENGTH_LONG).show();
                //progressDialog.dismiss();
                return;
            }
            try
            {
                JSONObject data = new JSONObject(result);
                String msgCode = data.getString("msgCode");
                String desc = data.getString("desc");
                if("1".equals(msgCode))
                {
                    JSONObject object = data.getJSONObject("msgBody");
                    tvUserName.setText(object.getString("userName"));
                    tvPoint.setText(object.getString("point"));
                    tvUserTitle.setText(object.getString("userTitle"));
                    tvRemark.setText(object.getString("remark"));
                    
                    UserInfo user = new UserInfo();
                    user.setUserName(object.getString("userName"));
                    user.setHeadPic(object.getString("headPic"));
                    user.setPoint(object.getInt("point"));
                    user.setRemark(object.getString("remark"));
                    user.setUserTitle(object.getString("userTitle"));
                    user.setUserType(object.getInt("userType"));
                    app.setUserInfo(user);
                }else
                {
                    Toast.makeText(MoreActivity.this, desc, Toast.LENGTH_LONG).show();
                    LogUtil.d(desc);
                }      
            }
            catch (Exception e)
            {
                LogUtil.e("" ,e);
            }
            //关闭进度条窗口
            //progressDialog.dismiss();      
        }
        
    }
}
